#include <iostream>
#include <vector>
#include <algorithm>

int sparseStr(std::string str)
{
    int i = 0;
    int v = 0;
    while (i < str.size())
    {
        int num = 0;
        while (str[i] <= '9' && str[i] >= '0')
        {
            num = num * 10 + str[i] - '0';
            i++;
        }
        if (str[i] == 'G')
        {
            v += num * 1024;
        }
        else if (str[i] == 'T')
        {
            v += num * 1024 * 1024;
        }
        else if (str[i] == 'M')
        {
            v += num;
        }
        i++;
    }

    return v;
}

bool cmp(std::pair<std::string, int> a, std::pair<std::string, int> b)
{
    if (a.second < b.second)
    {
        return true;
    }
    else
    {
        return false;
    }
}

int main()
{
    int n;
    std::cin >> n;
    std::cin.ignore();

    std::vector<std::pair<std::string, int>> mem(n);
    for (int i = 0; i < n; i++)
    {
        std::string str;
        std::getline(std::cin, str);
        mem[i].first = str;
        mem[i].second = sparseStr(str);
    }

    std::sort(mem.begin(), mem.end(), cmp);

    for (int i = 0; i < n; i++)
    {
        std::cout << mem[i].first << " ";
    }
    std::cout << std::endl;

    return 0;
}
